package com.rsstuan.xmpp;

import java.util.List;

import com.rsstuan.data.TuanGouWebItem;
import com.rsstuan.service.ChannelService;
import com.rsstuan.service.ItemService;
import com.rsstuan.storage.data.TuanGouChannelData;

public class XMPPMsgProcessor {

	private String intro = "你好啊，我叫团团，是团四方的智能机器人";
	
	private String unknown = "我有点晕，不知道你在说什么";
	private List<TuanGouChannelData> channels;
	
	private List<TuanGouWebItem> items;
	
	private static XMPPMsgProcessor instance = new XMPPMsgProcessor();
	public static XMPPMsgProcessor getInstance() {
		return instance;
	}
	public void XMPPMsgProcessor() {

	}
	public String answer(String msg) {
		
		if (msg.length() < 5 && msg.startsWith("你好")) {
			return intro;
		}
		if (msg.contains("团购") && msg.contains("什么")) {
			channels = ChannelService.getInstance().getAllChannels();
			items = ItemService.getInstance().getAllAcitveItems();
		TuanGouChannelData qc = null;
		for (TuanGouChannelData channel : channels ) {
			String title = channel.getTitle();
			if (title != null && title.length() > 2) {
				title = title.substring(0, 2);
			}
			if (title != null && msg.contains(title)) {
				qc = channel;
				break;
			}
		}
		if (qc != null) {
			TuanGouWebItem qi = null;
			for (TuanGouWebItem item : items) {
				if (item.getChannelId() != null
						&& item.getChannelId().equals(qc.getId())) {
					qi = item;
					break;
				}
			}
			if (qi != null) {
				StringBuilder sb = new StringBuilder();
				sb.append(qc.getTitle()); 
				sb.append("今天的团购是: ");
				sb.append(qi.getTitle());
				sb.append(" 原价:");
				sb.append(qi.getOrigPrice());
				sb.append(" 现价:");
				sb.append(qi.getPrice());
				return sb.toString();
			}
		}
		}
		return unknown;
	}
}
